package com.activity;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.misc.Tool;

public class CreateTopicActivity extends Activity {
	// constant
	static final int SEND_CREATED_TOPIC = 3;

	// field
	EditText etTitle, etContent;
	Button btnPhoto;
	Button btnDone;
	Bitmap bitmap;
	Boolean isTitleFilled = false, isContentFilled = false;
	Long myPhoneNumber;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_activity_page);
		
		myPhoneNumber = getIntent().getLongExtra(MainActivity.FRIEND_PHONE_NUMBER, 0);
		bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.activity_photo_default_empty)).getBitmap();
		
		etTitle = (EditText) findViewById(R.id.etTitle);
		etContent = (EditText) findViewById(R.id.etContent);
		btnDone = (Button) findViewById(R.id.btnDone);
		
		etTitle.addTextChangedListener(new TextWatcher() {
	        public void onTextChanged(CharSequence s, int start, int before, int count) {
	        	isTitleFilled = !TextUtils.isEmpty(s);
				btnDone.setEnabled(isTitleFilled && isContentFilled);
	        }
	        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	        }
	        public void afterTextChanged(Editable s) {
	        }
	    });
		etContent.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				isContentFilled = !TextUtils.isEmpty(s);
				btnDone.setEnabled(isTitleFilled && isContentFilled);
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			public void afterTextChanged(Editable s) {
			}
		});
		
		btnPhoto = (Button) findViewById(R.id.btnPhoto);
		btnPhoto.setHintTextColor(Color.BLUE);
		btnPhoto.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.setType("image/*");
				intent.putExtra("return-data", true);
				startActivityForResult(intent, 0);
			}
		});
		
		btnDone.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				sendCreatedTopic();
				finish();
			}
		});
		((Button) findViewById(R.id.btnCancel)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == 0 && intent != null) {
			bitmap = intent.getParcelableExtra("data");
			Matrix matrix = new Matrix();
			matrix.postScale(150 / (float) bitmap.getWidth(), 130 / (float) bitmap.getHeight());
			btnPhoto.setBackgroundDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true)));
			btnPhoto.setHint("");
		}
	}
	
	
	void sendCreatedTopic() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Socket socket = null;
				DataOutputStream dos = null;
				DataInputStream dis = null;
				try {
					socket = new Socket(MainActivity.SERVER_IP, 9999);
					dos = new DataOutputStream(socket.getOutputStream());
					dis = new DataInputStream(socket.getInputStream());
					dos.writeInt(SEND_CREATED_TOPIC);
					dos.writeLong(myPhoneNumber);
					dos.writeUTF(etTitle.getText().toString());
					dos.writeUTF(etContent.getText().toString());
					Tool.sendByteArray(dos, Tool.bitmap2ByteArray(bitmap));
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					try {
						dis.close();
						dos.close();
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
}
